package com.chinatgl.latte.ec.main.sort.content;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;
import com.chinatgl.latte.app.Latte;
import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.API;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.params.MapUtil;
import com.chinatgl.latte.net.NetRequest;
import com.chinatgl.latte.net.listener.onNetRquestListener;
import com.chinatgl.latte.util.json.Result;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;
import java.util.WeakHashMap;

import butterknife.BindView;


public class ContentDelegate extends LatteDelegate {

    private static final String ARG_CONTENT_ID = "CONTENT_ID";
    private String mContentId = null;


    @BindView(R2.id.rv_list_content)
    RecyclerView mRecyclerView = null;
    private List<SectionBeanDetail.ChildBrandsBean> childBrands;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mContentId = args.getString(ARG_CONTENT_ID);
        }
    }

    public static ContentDelegate newInstance(String contentId) {
        final Bundle args = new Bundle();
        args.putString(ARG_CONTENT_ID, contentId);
        final ContentDelegate delegate = new ContentDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_list_content;
    }

    private void initData() {
        System.out.println("接受的id是:" + mContentId);


        WeakHashMap<String, Object> params = MapUtil.getParams();

        params.put("catId", mContentId);
        Type type = new TypeToken<Result<SectionBeanDetail>>() {
        }.getType();

        NetRequest.post(API.CATDETAIL, params, type, true, 60 * 24, new onNetRquestListener() {
            @Override
            public void requestdataFinish(String catalog, Object bean) {

                if (bean != null) {
                    Result<SectionBeanDetail> result = (Result<SectionBeanDetail>) bean;
                    if (result.getCode() == 0) {
                        SectionBeanDetail result1 = result.getResult();
                        if (result1 != null) {
                            //子品牌的分类
                            childBrands = result1.getChildBrands();
                            final SectionAdapter sectionAdapter =
                                    new SectionAdapter(R.layout.item_section_content,
                                            childBrands);

                            mRecyclerView.setAdapter(sectionAdapter);
                        }


                    }
                }


            }

            @Override
            public void requestdataFailed(String msg, Throwable e) {

            }

            @Override
            public void requestComplete() {

            }
        },getActivity());


    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        final StaggeredGridLayoutManager manager =
                new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        initData();
        initListener();
    }

    private void initListener() {
        mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                String id = childBrands.get(i).getId();
                Toast.makeText(Latte.getApplication(), id, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemChildLongClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }
        });

    }
}
